﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T13DelegateAndEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 委托
            //委托可为空
            Buy buy = null;
            //定义对象
            DelegateAndBuy d = new DelegateAndBuy();
            //buy委托对象上有两个功能
            buy = new Buy(d.BuyApple);
            buy += new Buy(d.BuyOrange);
            //调用一次委托，可执行加载的两个功能
            Console.WriteLine(buy("张三"));
            //委托对象作为函数参数
            d.WhoBuy("李师师",buy);
            //buy对象上去掉某个功能
            buy -= d.BuyApple;
            d.WhoBuy("王卫东", buy);
            //取消委托
            buy = null;
            #endregion

            #region 事件
            //发布者对象
            EventBuy e = new EventBuy();
            //订阅者对象
            MrHu hu = new MrHu();
            MrXu xu = new MrXu();
            //订阅者---注册
            hu.Register(e);
            xu.Register(e);
            //发布者---发布消息
            e.Issue();
            #endregion

            #region 事件订阅
            Console.WriteLine("请输入你的取款金额：");
            string amount = Console.ReadLine();
            Console.WriteLine("交易成功，请取出银行卡");
            //事件参数
            UserEventArgs user = new UserEventArgs("ZZZ@126.com", "123456789", amount);
            SubscribeSystem sub = new SubscribeSystem(new Bank(), user);
            #endregion
            Console.ReadKey();
        }
    }
}
